var $ = require("../../utils/Ajax.js");
Page({
  data: {
    // 图片路径配置
    PictureUrl: getApp().PictureUrl,

    // 收货地址地址Id
    AddressId: 0,
    // 商品id属性id数量
    GidAidNum: [],
    OrderInfor: null,

    Type: null,

    // 配送方式
    DistriIndex: 0,
    Distribution: ["快递免邮", "到店自提", "我要存茶"],

    // 判断存茶地址是否显示
    TeaAddress: true,

    // 判断存茶信息是否显示
    TeaInfor: false,

    // 存茶时间
    TeaTime: "请选择存茶时间",
    TeaTimeId: 0,
    TeaMoney: 0,

    // 存茶
    showShopPopup: false,
    animationData: {},

    // 选择优惠券
    CouponId: 0,
    CouponSelect: "请选择优惠券",

    // 优惠券
    showShopPopup1: false,
    animationData1: {},

    // 是否可以开发票
    InvoiceId: 0,
    IfInvoice: "本次不开具发票",

    // 订单金额
    OrderInforShopPrice: 0
  },

  onLoad: function (options) {
    var BuyInfor = JSON.parse(options.BuyInfor);
    console.log(BuyInfor)
    var GidAidNum = [];
    GidAidNum.push(BuyInfor.carts.id + "_" + BuyInfor.carts.attr.id + "_" + 1)
    this.setData({
      AddressId: BuyInfor.address.id,
      GidAidNum: GidAidNum,  
      OrderInfor: BuyInfor,
      Type: options.Type,
      OrderInforShopPrice: BuyInfor.carts.shopPrice
    })
  },
  
  onShow: function () {
    var Invoice = wx.getStorageSync('Invoice');
    if (Invoice.length != 0) {
      this.setData({
        InvoiceId: Invoice.id,
        IfInvoice: "电子(" + Invoice.content + "-" + Invoice.title + ")"
      })
    }else{
      this.setData({
        IfInvoice: "本次不开具发票"
      })
    }
  },

  // 点击优惠券
  Coupon: function () {
    var animation = wx.createAnimation({
      duration: 400,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(834).step()
    this.setData({
      animationData1: animation.export(),
      showShopPopup1: true
    })
    setTimeout(() => {
      animation.translateY(0).step()
      this.setData({
        animationData1: animation.export()
      })
    }, 200)
  },

  // 选择优惠券
  CouponSelect: function (e) {
    var Id = e.detail.value.split("_");
    console.log(Id);
    this.setData({
      CouponId: Id[0],
      CouponSelect: Id[1]
    })
  },

  // 关闭优惠券
  CouponClose: function (e) {
    var Status = e.currentTarget.dataset.status;
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(834).step()
    this.setData({
      animationData1: animation.export(),
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData1: animation.export(),
        showShopPopup1: false
      })
    }.bind(this), 200)
    if (Status == 1) {

    } else {
      this.setData({
        CouponSelect: "请选择优惠券"
      })
    }
  },

  // 点击选择配送方式
  ClickDistri: function (e) {
    var Index = e.currentTarget.dataset.index_;
    this.setData({
      DistriIndex: Index,
      OrderInforShopPrice: this.data.OrderInfor.carts.shopPrice
    })
    switch (Index) {
      case 0:
        this.setData({
          TeaAddress: true,
          DistriIndex: Index,
          TeaInfor: false,
          TeaTime: "请选择存茶时间",
          TeaTimeId: 0,
          TeaMoney: 0,
        })
        break;
      case 1:
        this.setData({
          TeaAddress: false,
          DistriIndex: Index,
          TeaInfor: false
        })
        break;
      case 2:
        this.setData({
          TeaNumber: false,
          DistriIndex: Index,
          TeaInfor: true
        })
        break;
    }
  },

  // 点击存茶时间
  StoreTea: function () {
    var animation = wx.createAnimation({
      duration: 400,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(452).step()
    this.setData({
      animationData: animation.export(),
      showShopPopup: true
    })
    setTimeout(() => {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export()
      })
    }, 200)
  },

  // 选择存茶时间
  TeaTimeSelect: function (e) {
    var Id = e.detail.value.split("_");
    this.setData({
      TeaTimeId: Id[0],
      TeaTime: Id[1],
      TeaMoney: Id[2]
    })
  },

  // 关闭存茶时间
  StoreTeaClose: function (e) {
    var Status = e.currentTarget.dataset.status;
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(452).step()
    this.setData({
      animationData: animation.export(),
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export(),
        showShopPopup: false
      })
    }.bind(this), 200)
    if (Status == 1) {
      var TeaMoney = this.data.TeaMoney;
      var OrderNumber = this.data.OrderInfor.carts.box;
      var OrderMoney = this.data.OrderInfor.carts.shopPrice;
      this.setData({
        OrderInforShopPrice: Number(OrderMoney) * 1 + Number(OrderNumber) * Number(TeaMoney)
      })
    } else {
      var OrderMoney = this.data.OrderMoney;
      this.setData({
        TeaTimeId: 0,
        TeaTime: "请选择存茶时间",
        TeaMoney: 0
      })
    }
  },

  // 点击发票
  Invoice: function () {
    wx.navigateTo({
      url: '../Invoice/Invoice',
    })
  },

  // 点击确认付款
  PaymentSubmit: function () {
    var that = this;
    var gan = that.data.GidAidNum;
    var costId = that.data.TeaTimeId;//存茶时间
    var couponId = that.data.CouponId;//优惠券
    var invoiceId = that.data.InvoiceId;//发票
    var orderType = that.data.DistriIndex;//配送方式
    var addressId = that.data.AddressId;//地址ID
    var isActivity = that.data.Type;
    $.Ajax("api/makeOrder", "POST", {
      gan: gan,
      costId: costId,
      couponId: couponId,
      invoiceId: invoiceId,
      kinds: 0,
      orderType: orderType,
      isActivity: isActivity,
      addressId: addressId,
      activityId: 0
    }, function (res) {
      if(res.s){
        wx.requestPayment({
          timeStamp: res.d.config.timestamp,
          nonceStr: res.d.config.nonceStr,
          package: res.d.config.package,
          signType: res.d.config.signType,
          paySign: res.d.config.paySign,
          success: function (res) {
            wx.navigateTo({
              url: '../MyOrder/MyOrder?Status=1',
            })
          },
          fail: function (res) {
            wx.showToast({
              title: "取消支付",
              icon: 'success',
              duration: 1500
            })
          }
        })
      }else{
        wx.showToast({
          title: res.m,
          icon: 'success',
          duration: 2000
        })
      }
    })
  }
})